package com.block.product.service.union;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.service.LogFactory;
import com.block.common.vo.ResponseVo;
import com.block.db.common.vo.BaseUserInfoVo;
import com.block.db.entity.ProductInfo;
import com.block.product.service.IUnionProductService;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 好汇推
 */
@Service
public class HaoHuiTuiProductServiceImpl implements IUnionProductService {

    private static Logger log = LogFactory.getCommLog();

    @Override
    public ResponseVo matchUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {

            String requestMatchUrl = productInfo.getChannelMatchUrl();
            HttpRequest httpRequest = HttpUtil.createPost(requestMatchUrl);
            httpRequest.header("Content-Type", "application/json");

            String mobile = userInfoVo.getMobile();
            String channelKey = productInfo.getChannelKey();
            String telephone = SecureUtil.aes(channelKey.getBytes()).encryptBase64(mobile);

            Map<String, Object> bizMap = new HashMap<>();
            bizMap.put("telephone", telephone);
            bizMap.put("channelCode", productInfo.getChannelCode());

            httpRequest.body(JSON.toJSONString(bizMap));
            String resultStr = httpRequest.execute().body();
            log.info("【产品联登】【好汇推】撞库完成【resultStr】{}【REQUEST_URL】{}【bizMap】{}",
                    resultStr, requestMatchUrl, JSON.toJSONString(bizMap));

            JSONObject jsonObject = JSON.parseObject(resultStr);
            String returnCode = jsonObject.getJSONObject("head").getString("returnCode");
            if (!ObjectUtil.equals("000000", returnCode)) {
                return new ResponseVo(AppRspCodeConstant.THRID_FAILED);
            }

            return new ResponseVo(AppRspCodeConstant.SUCCESS);
    }

    @Override
    public ResponseVo registerUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {

            String requestRegisterUrl = productInfo.getChannelRegisterUrl();

            HttpRequest httpRequest = HttpUtil.createPost(requestRegisterUrl);
            httpRequest.header("Content-Type", "application/json");

            String telephone = userInfoVo.getMobile();

            String ip = userInfoVo.getIp();
            String osType = "android";
            String osVersion = "10";
            String sign = String.format("{ telephone:\"%s\", ip:\"%s\", osType:\"%s\", osVersion:\"%s\"}",
                    telephone, ip, osType, osVersion);

            String channelKey = productInfo.getChannelKey();

            String newsign = SecureUtil.aes(channelKey.getBytes()).encryptBase64(sign);

            Map<String, Object> bizMap = new HashMap<>();
            bizMap.put("sign", newsign);
            bizMap.put("channelCode", productInfo.getChannelCode());


            httpRequest.body(JSON.toJSONString(bizMap));
            String resultStr = httpRequest.execute().body();
            log.info("【产品联登】【好汇推】【注册】注册完成【result】{}【MATCH_USER_URL】{}【bizMap】{}",
                    resultStr, requestRegisterUrl, JSON.toJSONString(bizMap));

            JSONObject jsonObject = JSON.parseObject(resultStr);
            JSONObject headObject = jsonObject.getJSONObject("head");
            String returnCode = headObject.getString("returnCode");
            String returnMessage = headObject.getString("returnMessage");

            if (ObjectUtil.equals("000000", returnCode)) {
                JSONObject body = jsonObject.getJSONObject("body");

                String result = body.getString("result");
                if (StrUtil.equals(result, "01")){
                    String url = body.getString("url");
                    JSONObject resultData = new JSONObject();
                    resultData.put("productUrl",url);
                    return new ResponseVo(AppRspCodeConstant.SUCCESS, resultData);
                }
            }
            return new ResponseVo(AppRspCodeConstant.THRID_FAILED, returnMessage, "");
    }
}
